package com.jz.community.moduleshoppingguide.home.ui.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jz.community.basecomm.base.BaseMvpFragment;
import com.jz.community.basecomm.constant.AppConstants;
import com.jz.community.commview.view.MyStateView;
import com.jz.community.moduleshoppingguide.R;
import com.jz.community.moduleshoppingguide.R2;
import com.jz.community.moduleshoppingguide.home.bean.CategoriesGoodsBean;
import com.jz.community.moduleshoppingguide.home.presenter.ShoppingGuidePagePresenter;
import com.jz.community.moduleshoppingguide.home.ui.ShoppingGuidePageView;
import com.jz.community.moduleshoppingguide.home.ui.adapter.ShoppingGuidePageAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;

import butterknife.BindView;

public class ShoppingGuidePageFragment extends BaseMvpFragment<ShoppingGuidePageView.View, ShoppingGuidePagePresenter> implements ShoppingGuidePageView.View, OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {

    @BindView(R2.id.rv_shopping_guide)
    RecyclerView rvShoppingGuide;
    @BindView(R2.id.shopping_guide_refresh)
    SmartRefreshLayout shoppingGuideRefresh;

    private String goodsCategoryId;
    private int page = 0;
    private int size = 10;
    private ShoppingGuidePageAdapter adapter;

    MyStateView myStateView;
    private View noDataView;
    private View errorView;
    private View loadingView;

    public static ShoppingGuidePageFragment newInstance(String goodsCategoryId) {
        Bundle bundle = new Bundle();
        bundle.putString("goodsCategoryId", goodsCategoryId);
        ShoppingGuidePageFragment fragment = new ShoppingGuidePageFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.module_shoppingguide_fragment_shopping_guide_page;
    }

    @Override
    protected ShoppingGuidePagePresenter createPresenter() {
        return new ShoppingGuidePagePresenter(this);
    }


    @Override
    protected void initDatas() {
        shoppingGuideRefresh.setOnRefreshListener(this);//刷新监听
        shoppingGuideRefresh.setEnableLoadMore(false);
        rvShoppingGuide.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        adapter = new ShoppingGuidePageAdapter(R.layout.module_shoppingguide_item_shopping_guide, new ArrayList<>());
        rvShoppingGuide.setAdapter(adapter);
        adapter.setOnLoadMoreListener(this, rvShoppingGuide);

        myStateView = new MyStateView();
        noDataView = myStateView.getNoDataView(getActivity(), (ViewGroup) rvShoppingGuide.getParent(), "暂无商品！", null);
        loadingView = myStateView.getLoadingView(getActivity(), (ViewGroup) rvShoppingGuide.getParent(), R.drawable.loading_default);

        errorView = myStateView.getErrorView(getActivity(), (ViewGroup) rvShoppingGuide.getParent(), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                page = 0;
                adapter.setEmptyView(loadingView);
                mPresenter.getCategorysGoodsInfoes(goodsCategoryId, page, size, AppConstants.ONREFRESH);
            }
        });
    }

    @Override
    protected void initLazy(@Nullable Bundle savedInstanceState) {
        super.initLazy(savedInstanceState);
        goodsCategoryId = getArguments().getString("goodsCategoryId");
        if (!TextUtils.isEmpty(goodsCategoryId)) {
            page = 0;
            mPresenter.getCategorysGoodsInfoes(goodsCategoryId, page, size, AppConstants.ONREFRESH);
        } else {
            adapter.setEmptyView(noDataView);
        }
    }

    @Override
    public void showCategorysGoodsInfoes(CategoriesGoodsBean baseGoodsInfo, int type) {
        shoppingGuideRefresh.finishRefresh();
        if (baseGoodsInfo != null && baseGoodsInfo.get_embedded() != null && baseGoodsInfo.get_embedded().getContent() != null && baseGoodsInfo.get_embedded().getContent().size() > 0) {
            if (type == AppConstants.ONREFRESH) {
                adapter.setNewData(baseGoodsInfo.get_embedded().getContent());
            } else {
                adapter.addData(baseGoodsInfo.get_embedded().getContent());
                adapter.loadMoreComplete();
            }
        } else {
            adapter.loadMoreEnd();
        }
        if (adapter.getItemCount() == 0) {
            adapter.setEmptyView(noDataView);
        }
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        if (!TextUtils.isEmpty(goodsCategoryId)) {
            page = 0;
            mPresenter.getCategorysGoodsInfoes(goodsCategoryId, page, size, AppConstants.ONREFRESH);
        } else {
            adapter.setEmptyView(noDataView);
            adapter.loadMoreEnd();
            shoppingGuideRefresh.finishRefresh();
        }
    }

    @Override
    public void onLoadMoreRequested() {
        if (!TextUtils.isEmpty(goodsCategoryId)) {
            page++;
            mPresenter.getCategorysGoodsInfoes(goodsCategoryId, page, size, AppConstants.ONLOADMORE);
        } else {
            adapter.setEmptyView(noDataView);
            adapter.loadMoreEnd();
            shoppingGuideRefresh.finishRefresh();
        }
    }

    @Override
    public void onFail(String s, int code) {
        if (code == 404) {
            adapter.setEmptyView(noDataView);
        } else {
            adapter.setEmptyView(errorView);
        }
        adapter.loadMoreEnd();
        shoppingGuideRefresh.finishRefresh();
    }
}
